package com.drore.logapp.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.google.gson.Gson;

/**
 * com.xjx.core.utils.SPUtil
 *
 * @author YuanChao <br/>
 *         create at 2015年6月22日 下午2:42:18
 */
public class SPUtil {
    private Gson gson;
    private SharedPreferences sp;

    public SPUtil(Context context) {
        sp = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public void putString(String key, String value) {
        sp.edit().putString(key, value).commit();
    }

    public String getString(String key) {
        return sp.getString(key, null);
    }

    public String getString(String key, String defValue) {
        return sp.getString(key, defValue);
    }
    public void putLong(String key, long value) {
        sp.edit().putLong(key, value).commit();
    }
    //取long类型值
    public long getLong(String key, long defValue) {
        return sp.getLong(key, defValue);
    }

    public void putBoolean(String key, boolean value) {
        sp.edit().putBoolean(key, value).commit();
    }

    public boolean getBoolean(String key) {
        return sp.getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    public void putObject(String key, Object value) {
        Gson gson = new Gson();
        String objJson = gson.toJson(value);
        sp.edit().putString(key, objJson).commit();
    }

    public Object getObject(String key, Class clazz) {
        return getGson().fromJson(sp.getString(key, ""), clazz);
    }

    public Gson getGson() {
        if (gson == null)
            gson = new Gson();
        return gson;
    }

    public SharedPreferences getSp() {
        return sp;
    }
}
